<!-- 样品检测内容 -->
<template>
    <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" label="编号" width="50"></el-table-column>
        <el-table-column prop="testType" label="检测类型" width="150">
            <template slot-scope="scope">{{getType(scope.row.testType)}}</template>
        </el-table-column>

        <el-table-column prop="fieldName" label="检测项目" width="150"></el-table-column>
        <el-table-column label="检测位置" width="150">
         <!--   <template slot-scope="scope">{{getPosition(scope.row.testPart)}}</template>-->
  <template slot-scope="scope">
         <el-select v-if="scope.row"  v-model="scope.row.position" filterable placeholder="请选择" @change='getPositionStr(scope.row)' style="width: 200px;">
                <el-option v-for="item in positionList" :key="item.id" :label="item.name" :value="item.code"></el-option>
              </el-select>
                <span v-else>{{scope.row.positionStr}}</span>             </template>
        </el-table-column>
        <el-table-column label="技术要求" prop='standard' width="150">
            <template slot-scope="scope">{{scope.row.standardName}}</template>
        </el-table-column>

        <el-table-column label="条件" prop='operator' width="150">
            <template slot-scope="scope">{{scope.row.operatorStr}}</template>
        </el-table-column>
         <el-table-column label="值1" prop='col1' width="150">
            <template slot-scope="scope">
                <el-input :controls='false' style='text-align:left'  v-model="scope.row.col1" :disabled='getStatus(scope.row,1)' @change="changeCol1(scope.row)" v-if="scope.row.operator!='choose'"></el-input>

                <span v-else>符合</span>
            </template>
        </el-table-column>
        <el-table-column label="值2" prop='col2' width="150">
            <template slot-scope="scope">
                <el-input :controls='false' style='text-align:left' v-model="scope.row.col2" :disabled='getStatus(scope.row,2)' @change="changeCol2(scope.row)" v-if="scope.row.operator!='choose'"></el-input>

                <span v-else>不符合</span>
            </template>
        </el-table-column>

        <el-table-column prop="unit" label="单位" width="150">
            <template slot-scope="scope">{{scope.row.unit}}</template>
        </el-table-column>

       <!-- <el-table-column prop="device" label="检测设备" width="150">

        </el-table-column>
        <el-table-column prop="remark" label="备注" width="150">

        </el-table-column>-->
        <el-table-column prop="operate" label="操作">
            <template slot-scope="scope">
                <!-- <el-button v-if="scope.row.father==7" size="mini">新增</el-button> -->
                <el-button size="mini" @click="deleteSubItem(scope.row.id)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>

</template>

<script>
    import {
        getPositionList,
        getTypeList
    } from '../service/task'
    import {
        getLableFromList
    } from '../utils/common'
    export default {
        props: ['tableData'],
        data() {
            return {
                testTypeList: [],
                positionList: [],
                testPartList: []
            }
        },
        methods: {
            getStatus(row, seq) {
                if (row.operator == 'range') {
                    return false;
                } else if (row.operator == 'choose' || row.operator == 'equal') {
                    return true;
                } else {
                    if (seq == 1) {
                        return false;
                    } else {
                        return true;
                    }
                }
            },
            changeCol1(row) {
                if (row.operator == 'range') {
                    if (row.orginalCol1) {
                        if (Number(row.col1) < Number(row.orginalCol1)) {
                            this.$alert('下限应该检测标准范围内', "提示", {
                                type: "error"
                            });
                            return false;
                        }
                        if (row.orginalCol2 && Number(row.col1) > Number(row.orginalCol2)) {
                            this.$alert('下限应该检测标准范围内', "提示", {
                                type: "error"
                            });
                            return false;
                        }
                    }
                } else if (row.operator == 'ge' || row.operator == 'gr') {
                    if (row.orginalCol1) {
                        if (Number(row.col1) < Number(row.orginalCol1)) {
                            this.$alert('下限应该检测标准范围内', "提示", {
                                type: "error"
                            });
                            return false;
                        }
                    }
                } else if (row.operator == 'le' || row.operator == 'less') {
                    if (row.orginalCol1) {
                        if (Number(row.col1) > Number(row.orginalCol1)) {
                            this.$alert('下限应该检测标准范围内', "提示", {
                                type: "error"
                            });
                            return false;
                        }
                    }
                }
                return true;
            },
            changeCol2(row) {
                if (Number(row.col2) < Number(row.orginalCol1) || Number(row.col2) > Number(row.orginalCol2)) {
                    this.$alert('字段2应该原区间范围内', '提示', {
                        type: 'error'
                    })
                    return false;
                }
                return true;
            },
            addColumn() {
                console.log('5');
            },
            getType(type) {
                return getLableFromList(this.testTypeList, type);
            },
            getPosition(type) {
                return getLableFromList(this.testPartList, type);
            },
            deleteColumn() {
                this.addColumnLabel = 5;
            },
            handleChange(val) {
                console.log(val);
            },
            // 编辑检测要求
            editTestingRequirements(id) {
                console.log(id);
                this.editTestingRequirementsId = id;
            },
            // 确认检测要求
            confirmTestingRequirements() {
                this.editTestingRequirementsId = 0;
            },


            // 删除
            deleteMainItem(id) {
                var that = this;
                let idArray = [];
                let deleteKey = 0;
                Object.keys(this.tableData).forEach(function(key) {
                    if (that.tableData[key].father == id && that.tableData[key].father != 0) {
                        // that.tableData.splice(key,1);
                        idArray.push(that.tableData[key].id);
                    }
                });
                console.log(idArray);
                if (idArray.length > 1) {
                    Object.keys(this.tableData).forEach(function(key) {
                        if (that.tableData[key].id == idArray[idArray.length - 1]) {
                            deleteKey = key;
                        }
                    });
                }
                if (deleteKey != 0) {
                    that.tableData.splice(deleteKey, 1);
                }
            },
            // 删除
            deleteSubItem(id) {
                var that = this;

                for (let i = 0; i < this.tableData.length; i++) {

                    if (that.tableData[i].id == id) {
                        that.tableData.splice(i, 1);
                        return;
                    }
                }
            }
        },
        mounted() {
            console.log(this.tableData);
            getTypeList().then(res => {
                this.testTypeList = res.data.result;
            });
             getPositionList().then(res => {
                this.testPartList = res.data.result;
               this.positionList = res.data.result;

            });
        }
    }

</script>
